package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.DishDto;
import com.itheima.pojo.Dish_flavor;
import com.itheima.result.R;
import com.itheima.service.ICategoryService;
import com.itheima.service.IDishService;
import com.itheima.service.IDish_flavorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜品管理 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-11-28
 */
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private IDishService sc;
    @Autowired
    private IDish_flavorService sc2;
    @Autowired
    private ICategoryService sc1;

    @GetMapping("/page")
    public R<Page<DishDto>> findPageOfDish(Integer page, Integer pageSize) {
        Page<Dish> dishPage = sc.page(new Page<Dish>(page, pageSize));
        ArrayList<DishDto> dtos = new ArrayList<>();
        for (Dish dish : dishPage.getRecords()) {
            DishDto dishDto = new DishDto();
            Category category = sc1.getOne(new LambdaQueryWrapper<Category>().eq(Category::getId, dish.getCategory_id()));
            dishDto.setCategoryName(category.getName());
            BeanUtils.copyProperties(dish, dishDto);
            dtos.add(dishDto);
        }
        Page<DishDto> dtoPage = new Page<DishDto>();
        dtoPage.setRecords(dtos);
        dtoPage.setTotal(dishPage.getTotal());
        return R.success(dtoPage);

    }

    @PostMapping
    public R<Boolean> addDish(@RequestBody DishDto dd) {
        boolean result = sc.save(dd);
        if (result) {
            return R.success(result);
        }
        return R.error("添加菜品失败");

    }

    @GetMapping("/{dishId}")
    public R<DishDto> getDish(@PathVariable Long dishId) {
        Dish dish = sc.getById(dishId);
        List<Dish_flavor> list = sc2.list(new LambdaQueryWrapper<Dish_flavor>().eq(Dish_flavor::getDish_id, dishId));
        Category category = sc1.getOne(new LambdaQueryWrapper<Category>().eq(Category::getId, dish.getCategory_id()));
        DishDto dishDto = new DishDto();
        dishDto.setDishFlavors(list);
        dishDto.setCategoryName(category.getName());
        BeanUtils.copyProperties(dish, dishDto);
        return R.success(dishDto);
    }

    @PutMapping
    public R<String> updateDish(@RequestBody DishDto dishDto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDto, dish);
        boolean a = sc.updateById(dish);
   /* 口味的修改比较独特，修改普遍根据id修改一条（替换除id外的属性），
     而口味的修改有多条，且修改后的条数可能会有所增减，甚至失去口味，故而采取特殊修改方式处理：
    先根据菜品id删掉该菜品所有口味，再根据提交上来的口味数据进行增加，增加可以为0；*/
        boolean r = sc2.remove(new LambdaQueryWrapper<Dish_flavor>().eq(Dish_flavor::getDish_id, dish.getId()));
        boolean s = sc2.saveBatch(dishDto.getDishFlavors());//失效？
        //if (a && r && s) {
            return R.success("修改菜品成功");
        //}
        //    return R.error("修改菜品失败");
        //}
    }}
